package edu.uwm.capstone.diasmart.types;

import java.util.ArrayList;
import java.util.Arrays;

import edu.uwm.capstone.diasmart.R;
import edu.uwm.capstone.util.DebugUtil;

import android.content.Context;

public class EntryTypeFactory {
	public static final int TYPE_BODYFAT = 8;
	public static final int TYPE_BP = 5;
	public static final int TYPE_EXERCISE = 2;
	public static final int TYPE_FOOD = 1;
	public static final int TYPE_GLUCOSE = 0;
	public static final int TYPE_HBA1C = 7;
	private static final EntryType[] TYPE_INSTANCES = new EntryType[9];
	public static final int[] TYPE_LIST = new int[9];
	public static final int[] TYPE_LIST_RESID;
	private static final String[] TYPE_LIST_STRING = new String[TYPE_LIST.length];
	public static final int TYPE_MEDICATION = 3;
	public static final int TYPE_PULSE = 6;
	public static final int TYPE_WEIGHT = 4;
	private static boolean initialized = false;
	static {
		TYPE_LIST[0] = 0;
		TYPE_LIST[1] = 1;
		TYPE_LIST[2] = 2;
		TYPE_LIST[3] = 3;
		TYPE_LIST[4] = 4;
		TYPE_LIST[5] = 5;
		TYPE_LIST[6] = 6;
		TYPE_LIST[7] = 7;
		TYPE_LIST[8] = 8;

		TYPE_LIST_RESID = new int[] { R.string.glucose, R.string.food,
				R.string.exercise, R.string.medication, R.string.weight,
				R.string.bp, R.string.pulse, R.string.hba1c, R.string.body_fat };

		TYPE_INSTANCES[0] = new GlucoseType();
		TYPE_INSTANCES[1] = new FoodType();
		TYPE_INSTANCES[2] = new ExerciseType();
		TYPE_INSTANCES[3] = new MedicationType();
		TYPE_INSTANCES[4] = new WeightType();
		TYPE_INSTANCES[5] = new BPType();
		TYPE_INSTANCES[6] = new PulseType();
		TYPE_INSTANCES[7] = new HbA1cType();
		TYPE_INSTANCES[8] = new BodyFatType();
	}

	private static void doInitialize(Context paramContext) {
		for (int i = 0;; i++) {
			if (i >= TYPE_LIST.length) {
				initialized = true;
				return;
			}
			TYPE_LIST_STRING[TYPE_LIST[i]] = paramContext
					.getString(TYPE_LIST_RESID[TYPE_LIST[i]]);
		}
	}

	public static EntryType getInstance(int paramInt) {
		return TYPE_INSTANCES[paramInt];
	}

	public static EntryType getInstance(String paramString) {
		for (int i = 0;; i++) {
			if (i >= TYPE_LIST_STRING.length)
				return null;
			if (paramString.equalsIgnoreCase(TYPE_LIST_STRING[i]))
				return TYPE_INSTANCES[i];
		}
	}

	public static String getTypeString(int paramInt) {
		return TYPE_LIST_STRING[paramInt];
	}

	public static String[] getTypeStrings() {
		return TYPE_LIST_STRING;
	}

	
	public static EntryType[] getVisibleTypes(EntryContext entryContext) {
		ArrayList<EntryType> entryTypeList = new ArrayList<EntryType>();
		for (int i = 0;i < TYPE_LIST.length; i++) {				
			if (entryContext.getPreferences().getBoolean(
					entryContext.getContext().getString(
							R.string.global_view_type_key_prefix)
							+ TYPE_LIST[i], true))
				entryTypeList.add(TYPE_INSTANCES[i]);
		}
		DebugUtil.debug("Diasmart","entryType"+Arrays.toString((EntryType[])entryTypeList.toArray(new EntryType[entryTypeList.size()]) ));
		return (EntryType[]) entryTypeList.toArray(new EntryType[entryTypeList.size()]);
	}

	public static void initialize(Context paramContext) {
		if (!initialized)
			doInitialize(paramContext);
	}

	public static boolean isTypeVisible(EntryContext paramEntryContext,
			int paramInt) {
		return paramEntryContext.getPreferences().getBoolean(
				paramEntryContext.getContext().getString(
						R.string.global_view_type_key_prefix)
						+ paramInt, true);
	}

	public static boolean isTypeVisible(EntryContext paramEntryContext,
			EntryType paramEntryType) {
		return isTypeVisible(paramEntryContext, paramEntryType.getType());
	}
}
